// pages/registandlogin/registandlogin.js
var util = require("../../utils/util.js");

Page({
  data: {
    loginBtnTxt: "登录",
    loginBtnBgBgColor: "#0099FF",
    btnLoading: false,
    disabled: false,
    inputUserName: '',
    inputPassword: '',
    avatarUrl: "../../images/logo1.png",
    logIcon: "../../images/logI.png",
    pwdIcon: "../../images/pwdI.png",
    formData: "",
    userInfo: null
  },

  formSubmit: function (e) {
    var param = e.detail.value;
    // console.log(param);
    this.setData({
      formData: param
    })
    this.mysubmit(param);
  },
  mysubmit: function (param) {
    var that = this;
    // console.log(param)
    var flag = util.checkPhoneNum(param.username, that) &&  util.checkPassword(param.password, that);

    if (flag) {
      this.setLoginData1();
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type: 'login',
          paras: this.data.formData
        },
        header: { 'content-type': 'application/json' },
        method: 'GET',
        success: function (res) {
          // success
          console.log(res)
          if (!res.data.errcode) {
            that.redirectTo(param);
            wx.setStorage({
              key: 'userInfo',
              data: {
                uid: res.data.str[0]['uid'],
                nickname: res.data.str[0]['nickname'],
                headImg: res.data.str[0]['headUrl'] == "" ? "http://1.aiym.applinzi.com/images/boy.png" : res.data.str[0]['headUrl'],
                // userInfo: res.data.str[0],
                userType: res.data.str[0]['userType'],
                userRank: res.data.str[0]['userRank'],
                sex: res.data.str[0]['sex']
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '用户名或密码有误，请重新输入'
            });
            that.setLoginData2();
          }
        },
      })

    }
  },
  setLoginData1: function () {
    this.setData({
      loginBtnTxt: "登录中",
      disabled: !this.data.disabled,
      loginBtnBgBgColor: "#999",
      btnLoading: !this.data.btnLoading
    });
  },
  setLoginData2: function () {
    this.setData({
      loginBtnTxt: "登录",
      disabled: !this.data.disabled,
      loginBtnBgBgColor: "#0099FF",
      btnLoading: !this.data.btnLoading
    });
  },
  redirectTo: function (param) {
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    })
  }

})